﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient.Authentication;
using NFine.Code;
using NFine.Domain._02_ViewModel;
using NFine.Domain._03_Entity.InformationCenter;
using NFine.Domain._04_IRepository.InformationCenter;
using NFine.Repository.InformationCenter;
using NFine.Domain._03_Entity.Biz;
using NFine.Domain._04_IRepository.Biz;
using NFine.Repository.Biz;

namespace NFine.Application.InformationCenter
{
    public class SysMessagesApp
    {
        private ISysMessagesRepository messagesService = new SysMessagesRepository();
        private IBasicSettingRepository basicSettingService = new BasicSettingRepository();

        public List<View_Messages> GetList(Pagination pagination, bool isRead, bool isAdmin, string userId,
            string keyword = "")
        {
            return messagesService.GetMessageList(pagination, isRead, isAdmin, userId, keyword);
            /*var expression = ExtLinq.True<SysMessagesEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_Title.Contains(keyword));
            }
            if (!isAdmin)
            {
                expression = expression.And(t => t.F_CreatorUserId == userId);
            }
            expression = expression.And(t => t.F_IsRead == isRead);
            var list = messagesService.FindList(expression, pagination).OrderByDescending(a => a.F_CreatorTime).ToList();

            return list;*/
        }

        public BasicSettingEntity GetBasicSetting()
        {
            var model = basicSettingService.FindEntity("ruibu");
            return model;
        }
        public List<SysMessagesEntity> GetMemberUnReadList(int topCount, string userId)
        {
            try
            {
                return messagesService.GetMemberUnReadList(topCount, userId);
            }
            catch (Exception ex)
            {
                return new List<SysMessagesEntity>();
            }
        }

        public SysMessagesEntity GetForm(string keyValue)
        {
            return messagesService.FindEntity(keyValue);
        }

        public View_Messages GetViewModel(string keyValue)
        {
            return messagesService.GetViewModel(keyValue);
        }

        public void SetMessageIsRead(string keyValue, bool isRead)
        {
            var entity = messagesService.FindEntity(keyValue);
            entity.F_IsRead = isRead;
            entity.Modify(keyValue);
            messagesService.Update(entity);
        }

        public void SubmitForm(SysMessagesEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Modify(keyValue);
                messagesService.Update(entity);
            }
            else
            {
                entity.Create();
                messagesService.Insert(entity);
            }
        }

        public int SubmitFormBaseSetting(BasicSettingEntity entity)
        {            
            if (!string.IsNullOrEmpty(entity.F_Id))
            {
                return basicSettingService.Update(entity);
            }
            else
            {
                entity.F_Id = Common.GuId();
                return basicSettingService.Insert(entity);
            }
        }

        public int DeleteMessages(string keyValues)
        {
            return messagesService.DeleteMessages(keyValues);
        }

        /// <summary>
        /// 获取导航图片
        /// </summary>
        /// <param name="appid"></param>
        /// <returns></returns>
        public NavigationImageEntity getImages()
        {
            return messagesService.getImages();
        }
        /// <summary>
        /// 保存导航图片
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public int saveImages(NavigationImageEntity entity)
        {
            return messagesService.saveImages(entity);
        }
    }
}